package com.da.javatest.leetCode;

/**
 * @author chenlida
 * @date 2018/12/20 15:43
 * @description
 */
public class Code821 {
    public static void main(String[] args) {
        new Code821().shortestToChar("aaba", 'b');
    }

    public int[] shortestToChar(String S, char C) {
        char[] chars = S.toCharArray();
        int[] result = new int[chars.length];
        int preIndex = -1;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == C) {
                preIndex = i;
                result[i] = 0;
            } else {
                if (preIndex == -1) {
                    result[i] = S.indexOf(C) - i;
                } else {
                    result[i] = S.indexOf(C, i) == -1 ? i - preIndex : Math.min(i - preIndex, S.indexOf(C, i) - i);
                }
            }
        }
        return result;
    }
}
